﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;
using System.Text;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 特产信息类
    /// </summary>
    public class CBizPushSpecialty : CustBizObject
    {
        private CDmDjkPushSpecialty m_CDmDjkPushSpecialty = new CDmDjkPushSpecialty();
        private CDmDjkPushSpecialty oDmDjkPushSpecialty
        {
            get { return m_CDmDjkPushSpecialty; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizPushSpecialty()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkPushSpecialty.DJK_PUSH_SPECIALTY);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["SPECIALTY_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageRows">每页多少条记录</param>
        /// <param name="pageNum">第几页</param>
        /// <param name="destionId">目的地编号</param>
        /// <param name="cityId">城市编号</param>
        /// <param name="countryId">国家编号</param>
        /// <returns></returns>
        public DataSet GetSpecialForPageview(int pageRows, int pageNum, string destionId, string cityId, string countryId)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("ipagerows", ParameterDirection.Input, EDbType.String, 36, pageRows));
            parmCollection.Add(new CDbParm("ipagenum", ParameterDirection.Input, EDbType.String, 36, pageNum));
            parmCollection.Add(new CDbParm("destination", ParameterDirection.Input, EDbType.String, 36, destionId));
            parmCollection.Add(new CDbParm("cityid", ParameterDirection.Input, EDbType.String, 36, cityId));
            parmCollection.Add(new CDbParm("countryid", ParameterDirection.Input, EDbType.String, 36, countryId));
            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_PUSH_SPECIALINFO_FOR_VIEW", parmCollection);

            return ds;
        }
        public DataSet GetSpecialForPageview(int pageRows, int pageNum, string destionId, string cityId, string countryId,string specialKey)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("ipagerows", ParameterDirection.Input, EDbType.String, 36, pageRows));
            parmCollection.Add(new CDbParm("ipagenum", ParameterDirection.Input, EDbType.String, 36, pageNum));
            parmCollection.Add(new CDbParm("destination", ParameterDirection.Input, EDbType.String, 36, destionId));
            parmCollection.Add(new CDbParm("cityid", ParameterDirection.Input, EDbType.String, 36, cityId));
            parmCollection.Add(new CDbParm("countryid", ParameterDirection.Input, EDbType.String, 36, countryId));
            parmCollection.Add(new CDbParm("specialKey", ParameterDirection.Input, EDbType.String, 36, specialKey));
            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_PUSH_SPECIALINFO_FOR_VIEW", parmCollection);

            return ds;
        }
        /// <summary>
        /// 获取特产信息
        /// </summary>
        /// <param name="destinId">目的地编号</param>
        /// <param name="cityId">城市编号</param>
        /// <param name="countryId">国家编号</param>
        /// <returns>SPECIALTY_ID：特产编号，SPECIALTY_NAME：特产名称，SPECIALTY_DEFAULT_PIC：特产图片</returns>
        public DataSet GetSpecialtyDataSet(string destinId, string cityId, string countryId)
        {
            //判断地区是否为空
            if (string.IsNullOrEmpty(destinId) && string.IsNullOrEmpty(cityId) && string.IsNullOrEmpty(countryId))
            {
                return null;
            }
            //SQL语句
            StringBuilder sbSqlString = new StringBuilder();
            sbSqlString.Append("SELECT SPECIALTY_ID AS ID,SPECIALTY_NAME AS NAME,SPECIALTY_DEFAULT_PIC AS PIC FROM    DJK_PUSH_SPECIALTY  WHERE SPECIALTY_IS_DELETED = 0 AND SPECIALTY_IS_DISPLAY = 1 ");
            //SQL参数
            CDbParmCollection parmCollection = new CDbParmCollection();
            #region 判断地区信息
            if (!string.IsNullOrEmpty(destinId))
            {
                sbSqlString.Append("AND SPECIALTY_DISTINATION=@DESTINID ");
                parmCollection.Add(new CDbParm("DESTINID", ParameterDirection.Input, EDbType.String, 36, destinId.Replace("'", "").Replace(",", "','")));
            }
            if (!string.IsNullOrEmpty(cityId))
            {
                sbSqlString.Append("AND SPECIALTY_CITY=@CITYID ");
                parmCollection.Add(new CDbParm("CITYID", ParameterDirection.Input, EDbType.String, 36, cityId.Replace("'", "").Replace(",", "','")));
            }
            if (!string.IsNullOrEmpty(countryId))
            {
                sbSqlString.Append("AND SPECIALTY_COUNTRY=@COUNTRYID ");
                parmCollection.Add(new CDbParm("COUNTRYID", ParameterDirection.Input, EDbType.String, 36, countryId.Replace("'", "").Replace(",", "','")));
            }
            #endregion
            sbSqlString.Append("ORDER BY SPECIALTY_IS_SPECIE DESC,SPECIALTY_UPLOAD_TIME DESC");
            DataSet ds = this.DataObj.QueryBySQL(sbSqlString.ToString());
            return ds;
        }

        /// <summary>
        /// 根据特产编号查询美食信息
        /// </summary>
        /// <param name="specialtyId">特产ID</param>
        /// <returns>SPECIALTY_ID:特产ID，SPECIALTY_NAME：特产名称，SPECIALTY_DEFAULT_PIC：特产图片，SPECIALTY_DETAILS：特产详情，SPECIALTY_DISTINATION：目的地编号，SPECIALTY_CITY：城市编号，SPECIALTY_COUNTRY：国家编号，COUNTRYNAME：国家名称，COUNTRY_CITY_NME：城市名称，DESTINATIONNAME：目的地名称</returns>
        public DataSet GetScenicById(string specialtyId)
        {
            if (string.IsNullOrEmpty(specialtyId))
            {
                return null;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("SPECIALTYID", ParameterDirection.Input, EDbType.String, 36, specialtyId.Replace("'", "").Replace(",", "','")));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT T3.*,A3.COUNTRY_CITY_NME DESTINATIONNAME FROM 
    (SELECT T2.*,A2.COUNTRY_CITY_NME CITYNAME FROM 
    (SELECT T1.*,A1.COUNTRY_CITY_NME COUNTRYNAME FROM 
    (SELECT S.SPECIALTY_ID AS ID ,S.SPECIALTY_NAME AS NAME ,S.SPECIALTY_DEFAULT_PIC PICTURE,S.SPECIALTY_DETAILS AS DETAILS,
    S.SPECIALTY_DISTINATION AS DISTIANTIONID ,S.SPECIALTY_CITY AS CITYID,
    S.SPECIALTY_COUNTRY AS COUNTRYID 
    FROM DJK_PUSH_SPECIALTY S
    WHERE S.SPECIALTY_ID=@SPECIALTYID ) T1 
    LEFT JOIN DJK_COUNTRY_CITY A1 
    ON T1.COUNTRYID=A1.COUNTRY_CITY_ID) T2 
    LEFT JOIN DJK_COUNTRY_CITY A2 
    ON T2.CITYID=A2.COUNTRY_CITY_ID) T3 
    LEFT JOIN DJK_COUNTRY_CITY A3 
    ON T3.DISTIANTIONID=A3.COUNTRY_CITY_ID", parmCollection);

            return ds;

        }

        /// <summary>
        /// 根据编号查询美食信息包含上一条下一条
        /// </summary>
        /// <param name="scenicId">特产编号</param>
        /// <returns>查询结果</returns>
        public DataSet GetSpecialByIdWithPrevNext(string spacialId)
        {
            if (string.IsNullOrEmpty(spacialId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("specialid", ParameterDirection.Input, EDbType.String, 36, spacialId));

            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_PREVIEW_SPECIEINFO_FOR_VIEW", parmCollection);

            return ds;
        }


        /// <summary>
        /// 发布特产信息
        /// </summary>
        /// <param name="specialtyName"></param>
        /// <param name="specialtyAddr"></param>
        /// <param name="specialtyPrice"></param>
        /// <param name="specialtyIsSpecie"></param>
        /// <param name="defaultPic"></param>
        /// <param name="desId"></param>
        /// <param name="cityId"></param>
        /// <param name="countryId"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        public string[] AddSpecialtyInfo(string specialtyName, string specialtyAddr, string specialtyPrice, bool specialtyIsSpecie, string defaultPic, string desId, string cityId, string countryId, string specialtyContent)
        {
            if (string.IsNullOrEmpty(specialtyName)
                || string.IsNullOrEmpty(defaultPic))
            {
                return new string[] { "false", "特产名称或封面图片不能为空" };
            }
            if (specialtyName.Length > 100)
            {
                return new string[] { "false", "特产名称需在100个字符以内" };
            }
            if (string.IsNullOrEmpty(desId)
                || string.IsNullOrEmpty(cityId)
                || string.IsNullOrEmpty(countryId))
            {
                return new string[] { "false", "地区信息有误，请检查" };
            }

            string strVpath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.strSaveUploadSysFileDirV + this.DataObj.NewID() + ".html";//存储文件的虚拟路径
            if (specialtyContent != string.Empty && strVpath != string.Empty && specialtyContent != "<br />")
            {
                string strFFullPath = System.Web.HttpContext.Current.Server.MapPath(strVpath);
                using (System.IO.TextWriter streamWriter = new System.IO.StreamWriter(strFFullPath))
                {
                    streamWriter.Write(specialtyContent);
                }
            }
            else
            {
                return new string[] { "false", "文件信息有误，请重试！" };
            }

            DataSet ds = new CDmDjkPushSpecialty();
            DataRow dr = ds.Tables[0].NewRow();

            dr["SPECIALTY_ID"] = this.DataObj.NewID();
            dr["SPECIALTY_NAME"] = specialtyName;
            dr["SPECIALTY_ADDRESS"] = specialtyAddr;
            dr["SPECIALTY_PRICE"] = specialtyPrice;
            dr["SPECIALTY_IS_SPECIE"] = specialtyIsSpecie;
            dr["SPECIALTY_DEFAULT_PIC"] = defaultPic;
            dr["SPECIALTY_UPLOAD_TIME"] = this.dtCurrDateTime;
            dr["SPECIALTY_DISTINATION"] = desId;
            dr["SPECIALTY_CITY"] = cityId;
            dr["SPECIALTY_COUNTRY"] = countryId;
            dr["CONTENT_FILE_URL"] = strVpath;
            dr["SPECIALTY_IS_DELETED"] = false;
            dr["SPECIALTY_IS_DISPLAY"] = true;
            dr["SPECIALTY_DETAILS"] = "TEST";

            ds.Tables[0].Rows.Add(dr);
            this.Update(ds);

            return new string[] { "true", "发布成功" };
        }


        public DataSet GetAllSpecialty()
        {
            DataSet ds = this.DataObj.QueryBySQL(@"SELECT * FROM DJK_PUSH_SPECIALTY");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }
    }
}
